package org.gap.service;

import java.util.List;
import java.util.Set;

import org.gap.dao.CaractPMDAO;
import org.gap.dao.TiersDAO;

import org.gap.domain.CaractPM;
import org.gap.domain.Tiers;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for CaractPM entities
 * 
 */

@Service("CaractPMService")
@Transactional
public class CaractPMServiceImpl implements CaractPMService {

	/**
	 * DAO injected by Spring that manages CaractPM entities
	 * 
	 */
	@Autowired
	private CaractPMDAO caractPMDAO;

	/**
	 * DAO injected by Spring that manages Tiers entities
	 * 
	 */
	@Autowired
	private TiersDAO tiersDAO;

	/**
	 * Instantiates a new CaractPMServiceImpl.
	 *
	 */
	public CaractPMServiceImpl() {
	}

	/**
	 * Return a count of all CaractPM entity
	 * 
	 */
	@Transactional
	public Integer countCaractPMs() {
		return ((Long) caractPMDAO.createQuerySingleResult("select count(o) from CaractPM o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing Tiers entity
	 * 
	 */
	@Transactional
	public CaractPM saveCaractPMTiers(Integer cpmTiers, Tiers related_tiers) {
		CaractPM caractpm = caractPMDAO.findCaractPMByPrimaryKey(cpmTiers, -1, -1);
		Tiers existingtiers = tiersDAO.findTiersByPrimaryKey(related_tiers.getTieTiers());

		// copy into the existing record to preserve existing relationships
		if (existingtiers != null) {
			existingtiers.setTieTiers(related_tiers.getTieTiers());
			existingtiers.setTieIdentClient(related_tiers.getTieIdentClient());
			existingtiers.setTieTypeTiers(related_tiers.getTieTypeTiers());
			existingtiers.setTieLibExterne(related_tiers.getTieLibExterne());
			existingtiers.setTieNationalite(related_tiers.getTieNationalite());
			existingtiers.setTieTelFix(related_tiers.getTieTelFix());
			existingtiers.setTieTelBureau(related_tiers.getTieTelBureau());
			existingtiers.setTieTelPortable(related_tiers.getTieTelPortable());
			existingtiers.setTieFax(related_tiers.getTieFax());
			existingtiers.setTieEMail(related_tiers.getTieEMail());
			existingtiers.setTieDateCreation(related_tiers.getTieDateCreation());
			existingtiers.setTieUserCreation(related_tiers.getTieUserCreation());
			related_tiers = existingtiers;
		} else {
			related_tiers = tiersDAO.store(related_tiers);
			tiersDAO.flush();
		}

		caractpm.setTiers(related_tiers);
		caractpm = caractPMDAO.store(caractpm);
		caractPMDAO.flush();

		related_tiers = tiersDAO.store(related_tiers);
		tiersDAO.flush();

		return caractpm;
	}

	/**
	 */
	@Transactional
	public CaractPM findCaractPMByPrimaryKey(Integer cpmTiers) {
		return caractPMDAO.findCaractPMByPrimaryKey(cpmTiers);
	}

	/**
	 * Load an existing CaractPM entity
	 * 
	 */
	@Transactional
	public Set<CaractPM> loadCaractPMs() {
		return caractPMDAO.findAllCaractPMs();
	}

	/**
	 * Delete an existing Tiers entity
	 * 
	 */
	@Transactional
	public CaractPM deleteCaractPMTiers(Integer caractpm_cpmTiers, Integer related_tiers_tieTiers) {
		CaractPM caractpm = caractPMDAO.findCaractPMByPrimaryKey(caractpm_cpmTiers, -1, -1);
		Tiers related_tiers = tiersDAO.findTiersByPrimaryKey(related_tiers_tieTiers, -1, -1);

		caractpm.setTiers(null);
		caractpm = caractPMDAO.store(caractpm);
		caractPMDAO.flush();

		related_tiers = tiersDAO.store(related_tiers);
		tiersDAO.flush();

		tiersDAO.remove(related_tiers);
		tiersDAO.flush();

		return caractpm;
	}

	/**
	 * Delete an existing CaractPM entity
	 * 
	 */
	@Transactional
	public void deleteCaractPM(CaractPM caractpm) {
		caractPMDAO.remove(caractpm);
		caractPMDAO.flush();
	}

	/**
	 * Return all CaractPM entity
	 * 
	 */
	@Transactional
	public List<CaractPM> findAllCaractPMs(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<CaractPM>(caractPMDAO.findAllCaractPMs(startResult, maxRows));
	}

	/**
	 * Save an existing CaractPM entity
	 * 
	 */
	@Transactional
	public void saveCaractPM(CaractPM caractpm) {
		CaractPM existingCaractPM = caractPMDAO.findCaractPMByPrimaryKey(caractpm.getCpmTiers());

		if (existingCaractPM != null) {
			if (existingCaractPM != caractpm) {
				existingCaractPM.setCpmTiers(caractpm.getCpmTiers());
				existingCaractPM.setCpmNomSociete(caractpm.getCpmNomSociete());
				existingCaractPM.setCpmFormeJuridique(caractpm.getCpmFormeJuridique());
				existingCaractPM.setCpmCodeNaf(caractpm.getCpmCodeNaf());
				existingCaractPM.setCpmSiren(caractpm.getCpmSiren());
				existingCaractPM.setCpmSiret(caractpm.getCpmSiret());
				existingCaractPM.setCpmTiersContact(caractpm.getCpmTiersContact());
				existingCaractPM.setCpmQualiteSociete(caractpm.getCpmQualiteSociete());
				existingCaractPM.setCpmEtatSociete(caractpm.getCpmEtatSociete());
				existingCaractPM.setCpmChiffreAffaire(caractpm.getCpmChiffreAffaire());
				existingCaractPM.setCpmNbrSalarier(caractpm.getCpmNbrSalarier());
			}
			caractpm = caractPMDAO.store(existingCaractPM);
		} else {
			caractpm = caractPMDAO.store(caractpm);
		}
		caractPMDAO.flush();
	}
}
